<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\service;

use app\common\model\oms\Companies as CompanyModel;
use app\common\model\oms\Store as StoreModel;
use app\common\model\oms\StoreOptometryDevice as StoreOptometryDeviceModel;

/**
 * Description of DeviceService
 *
 * @author tommy.pan
 */
class DeviceService {

    /**
     * 获取公司已有的设备
     * @param type $params
     * @return type
     */
    public function getCompanyOptometryDeviceArray($params) {
        $companyModel = new CompanyModel();
        $resObj = $companyModel->getCompanyOptometryDeviceByCompanyCode($params['company_code']); //获取集团的设备
        $companyDeviceArray = collection($resObj)->toArray(); //对象转成数组
        $storeModel = new StoreModel();
        $storeObj = $storeModel->getStoreOptometryDeviceByStoreId($params['store_id']); //获取集团的设备
        $storeDeviceCodIdArray = collection($storeObj)->toArray(); //门店里已经使用的集团设备ID
        $data = $this->getDiffArray($companyDeviceArray, $storeDeviceCodIdArray); //可添加的设备是集团的减去门店的
        return $data;
    }

    /**
     * 获取门店跟公司设备的不同处
     * @param type $companyDeviceArray
     * @param type $storeDeviceCodIdArray
     * @return type
     */
    public function getDiffArray($companyDeviceArray, $storeDeviceCodIdArray) {
        foreach ($companyDeviceArray as $key => $val) {
            if (in_array($val['cod_id'], array_column($storeDeviceCodIdArray, 'cod_id'))) {
                $val['sod_id'] = $this->getSodId($val['cod_id'], $storeDeviceCodIdArray);
                $data['store_used'][] = $val;
                unset($companyDeviceArray[$key]);
            }
        }
        $data['able'] = array_values($companyDeviceArray);
        return $data;
    }

    /**
     * 通过传入的数组获取sodId
     * @param type $codId
     * @param type $storeDeviceCodIdArray
     */
    public function getSodId($codId, $storeDeviceCodIdArray) {
        foreach ($storeDeviceCodIdArray as $key => $value) {
            if ($codId == $value['cod_id']) {
                $sodId = $value['sod_id'];
                break;
            }
        }
        return $sodId;
    }

    /**
     * 修改机器使用状态
     * @param type $params
     * @return boolean
     */
    public function editStoreOptometryDeviceStatus($params) {
        $checkStoreOptometryDevice = StoreOptometryDeviceModel::get(['store_id' => $params['store_id'], 'cod_id' => $params['cod_id']]);
        if (false === empty($checkStoreOptometryDevice) && 1 == $params['action']) {//1是添加设备
            return false;
        }
        $data = StoreOptometryDeviceModel::get(['store_id' => $params['store_id'], 'id' => $params['sod_id']]);
        //通过action判断是新增还是删除
        if ('0' == $params['action']) {
            if (false === empty($data)) {
                StoreOptometryDeviceModel::destroy(['id' => $params['sod_id']]);
                return $data;
            }
            return false;
        }
        $time = time();
        $data = ['store_id' => $params['store_id'], 'cod_id' => $params['cod_id'], 'create_time' => $time, 'update_time' => $time];
        return StoreOptometryDeviceModel::create($data);
    }

}
